<?php

namespace App\Models;
use Cache;

class Menu extends BaseModel
{
    const CACHE_MENU_ALL = 'cache_menu_all';
    const CACHE_MENU_TREE = 'cache_menu_tree';

    protected $table = 'menu';


    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->_getOne($where);
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->getPaginate($where, $fields, $order,  $pageSize);
        } else {
            return $this->getAll($where, $fields, $order);
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->_del($where);
    }

    /**
     * 添加数据返回ID
     */
    public function add(array $data)
    {
        $res = $this->_add($data);
        $this->generateCache();
        return $res;
    }


    /**
     * 根据条件更新数据
     */
    public function up(array $where, array $data)
    {
        $res = $this->_updata($where, $data);
        $this->generateCache();
        return $res;
    }

    /**
     * 重新生成缓存
     */
    public function generateCache()
    {
        Cache::forget(self::CACHE_MENU_ALL);
        $where['status'] = 1;
        $order['sort'] = 'asc';
        $list = $this->getList($where,'*',$order);
        if ($list) {
            $list = $list->toArray();
        }
        Cache::forever(self::CACHE_MENU_ALL, $list);
        Cache::forget(self::CACHE_MENU_TREE);
        $tree = $this->getMenuTree();
        Cache::forever(self::CACHE_MENU_TREE, $tree);
    }

    /**
     * 获取缓存
     */
    public function getCacheMenuList()
    {

        if (Cache::has(self::CACHE_MENU_ALL)) {
            return Cache::get(self::CACHE_MENU_ALL);
        } else {
            $where['status'] = 1;
            $order['sort'] = 'asc';
            $list = $this->getList($where,'*',$order);
            if ($list) {
                $list = $list->toArray();
            }
            Cache::forever(self::CACHE_MENU_ALL, $list);
            return $list;
        }
    }

    public function getMenuLevelTree($pid = 0)
    {
        $categoryList = $this->getCacheMenuList();
        $categoryTree = levelTree($categoryList, 'id', 'pid', $pid);
        return $categoryTree;
    }


    public function getMenuTree($pid = 0)
    {
        if (Cache::has(self::CACHE_MENU_TREE)) {
            return Cache::get(self::CACHE_MENU_TREE);
        } else {
            $list = $this->getMenuLevelTree();
            if (!$list) {
                return false;
            }
            $list = sortArrayAsc(mult_unique($list), 'sort');
            $tree = $list;
            //对分类数组进行排序
            foreach ($tree as $k => $v) {
                if (isset($v['child']) && is_array($v['child'])) {
                    $tree[$k]['child'] = sortArrayAsc(mult_unique($v['child']));
                    if (isset($tree[$k]['child']) && is_array($tree[$k]['child'])) {
                        foreach ($tree[$k]['child'] as $j => $v1) {
                            if (isset($v1['child']) && is_array($v1['child'])) {
                                $tree[$k]['child'][$j]['child'] = sortArrayAsc(mult_unique($v1['child']));
                                if (isset($tree[$k]['child'][$j]['child']) && is_array($tree[$k]['child'][$j]['child'])) {
                                    foreach ($tree[$k]['child'][$j]['child'] as $m => $n) {
                                        if (isset($n['child']) && is_array($n['child'])) {
                                            $tree[$k]['child'][$j]['child'][$m]['child'] = sortArrayAsc(mult_unique($n['child']));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Cache::forever(self::CACHE_MENU_TREE, $tree);
            return $tree;
        }

    }
}
